package com.pocketsweet.ui;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLVoice;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.RecordButtonView;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.dialog_audio)
public class DialogAudio extends BaseActivity {
	@ViewInject(R.id.btnOk)
	private Button btnOk;

	@ViewInject(R.id.btnCancel)
	private Button btnCancel;

	@ViewInject(R.id.btnSave)
	private Button btnSave;

	@ViewInject(R.id.btnRecord)
	private RecordButtonView mRecordButtonView;

	private LoadingDailog loading;
	private String theme;
	private MLVoice mlVoice = new MLVoice();

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		Intent intent = getIntent();
		theme = intent.getStringExtra("theme");
	}

	@OnClick(R.id.btnSave)
	public void onBtnOkClick(View v) {
		int time = mRecordButtonView.getTime();
		String path = mRecordButtonView.getSoundPath().get(0);
		// Intent intent = getIntent();
		// intent.putExtra("choosedSex", choosedSex);
		// this.setResult(RESULT_OK, intent);
		if (mRecordButtonView.isRecording()) {
			ToolKits.toast(DialogAudio.this, "录音还没完成哦");
			return;
		}
		if (!mRecordButtonView.isRecorded()) {
			ToolKits.toast(DialogAudio.this, "您还没有上传语音哦");
			return;
		} else {
			try {
				Main.audioFile = AVFile.withAbsoluteLocalPath(
						"order_audio.amr", path);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		if (time <= 2) {
			ToolKits.toast(DialogAudio.this, "您上传的语言小于2s，无法提交");
			return;
		}
		if(time>120){
			ToolKits.toast(DialogAudio.this, "您上传的语言大于120s，请重新录音");
			return;
		}
		loading = ToolKits.createLoadingDialog(DialogAudio.this, "提交中..");
		loading.show();
		mlVoice.setVoice(Main.audioFile);
		mlVoice.setTime(time);
		mlVoice.setTheme(theme);
		mlVoice.saveInBackground(new SaveCallback() {

			@Override
			public void done(AVException arg0) {
				if (arg0 == null) {
					loading.dismiss();
					UserService.getCurrentUser().setIsHasVoice(true);
					UserService.getCurrentUser().saveInBackground();
					Main.voiceList.add(mlVoice);
					ToolKits.toast(DialogAudio.this, "上传成功");
					Intent intent = new Intent();
					intent.setAction(Main.ACTION_VOICE);
					sendBroadcast(intent);
					DialogAudio.this.finish();
				} else {
					loading.dismiss();
					ToolKits.toast(DialogAudio.this, "上传失败");
					DialogAudio.this.finish();

				}
			}
		});
	}

	@OnClick(R.id.btnCancel)
	public void onBtnCancelClick(View v) {
		// Intent intent = getIntent();
		// this.setResult(RESULT_CANCELED, intent);
		mRecordButtonView.destoryRecord();
		finish();
	}

	/**
	 * 重写onKeyDown方法
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// if (mRecordButtonView.isRecording()) {
			mRecordButtonView.destoryRecord();
			//
			// ToolKits.toast(DialogAudio.this, "您在录音中");
			// return false;
			// }
			return super.onKeyDown(keyCode, event);

		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
